const moment = require('moment');
module.exports = app => {
  const {STRING, INTEGER, DATE, BIGINT} = app.Sequelize;

  const Resource = app.model.define('source', {
    id: {
      type: BIGINT,
      primaryKey: true,
      autoIncrement: true,
    },
    name: STRING(100),
    url: STRING(100),
    type: BIGINT,
    folder_id: BIGINT,
    create_time: {
      type: DATE,
      get() {
        return moment(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
      }
    },
    modify_time: {
      type: DATE,
      get() {
        return moment(this.getDataValue('modify_time')).format('YYYY-MM-DD HH:mm:ss');
      }
    },
    download: INTEGER,
    view: INTEGER,
    size: {
      type: BIGINT,
      get() {
        return app.getSizeStr(this.getDataValue('size'));
      }
    },
    mime: STRING(100),
    delete_flag: INTEGER,
  }, {
    tableName: 'hy_source',
    timestamps: false,
  });
  
  Resource.associate = function() {
    app.model.Resource.belongsTo(app.model.Folder, {
      as: 'folder',
      foreignKey: 'folder_id',
      targetKey: 'id'
    });
  };

  return Resource;
};
